//json时间对象的格式化;
function jsonDateFormat(jsonDate, formatstr) {//json日期格式转换为正常格式
    try {
        var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
        return date.format(formatstr);
    } catch (ex) {
        return "";
    }
}
//时间对象格式化
function DateFormat(Date, formatstr) {
    try {
        var date = new Date(parseInt(Date.replace("/Date(", "").replace(")/", ""), 10));
        return date.format(formatstr);
    } catch (ex) {
        return "";
    }
}

//时间戳转时间字符串
function TimestampFormat(tm, formatstr) {
    try {
        var date = new Date(parseInt(tm) * 1000);
        return date.format(formatstr);
    } catch (ex) {
        return "";
    }
}
//纯数字时间转为年月日
function numToDate(datetime) {
    return datetime.substr(0, 4) + "年" + datetime.substr(4,2) + "月" + datetime.substr(6, 2) + "日 " + datetime.substr(8, 2) + ":" + datetime.substr(10, 2);
}

//--------------------------------------------------------------------------------------------
/*
* 指定时间加上指定天数或月数;
*/
function DateAdd(interval, number, date) {
    /* 
      *   功能:实现VBScript的DateAdd功能. 
      *   参数:interval,字符串表达式，表示要添加的时间间隔. 
      *   参数:number,数值表达式，表示要添加的时间间隔的个数. 
      *   参数:date,时间对象. 
      *   返回:新的时间对象. 
      *   var   now   =   new   Date(); 
      *   var   newDate   =   DateAdd( "d ",5,now); 
      *---------------   DateAdd(interval,number,date)   ----------------- 
      */
    switch (interval) {
        case "y ": {
            date.setFullYear(date.getFullYear() + number);
            return date;
            break;
        }
        case "q ": {
            date.setMonth(date.getMonth() + number * 3);
            return date;
            break;
        }
        case "m ": {
            date.setMonth(date.getMonth() + number);
            return date;
            break;
        }
        case "w ": {
            date.setDate(date.getDate() + number * 7);
            return date;
            break;
        }
        case "d ": {
            date.setDate(date.getDate() + number);
            return date;
            break;
        }
        case "h ": {
            date.setHours(date.getHours() + number);
            return date;
            break;
        }
        case "m ": {
            date.setMinutes(date.getMinutes() + number);
            return date;
            break;
        }
        case "s ": {
            date.setSeconds(date.getSeconds() + number);
            return date;
            break;
        }
        default: {
            date.setDate(d.getDate() + number);
            return date;
            break;
        }
    }
}
/*调用
var now = new Date();
//加五天.  
var newDate = DateAdd("d ", 5, now);
alert(newDate.toLocaleDateString())
//加两个月.  
newDate = DateAdd("m ", 2, now);
alert(newDate.toLocaleDateString())
//加一年  
newDate = DateAdd("y ", 1, now);
alert(newDate.toLocaleDateString())
*/
//--------------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------------
/*
* 时间对象的格式化;
*/
Date.prototype.format = function (format) {
    /*
    * eg:format="yyyy-MM-dd hh:mm:ss";
    */
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),   //day
        "h+": this.getHours(),  //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
/*
*调用方法
var times = 1290371638000;
var testDate = new Date(times);
var testStr = testDate.format("yyyy年MM月dd日hh小时mm分ss秒");
var testStr2 = testDate.format("yyyy-MM-dd");
*/
//--------------------------------------------------------------------------------------------